#include "net_pre.h"
#include "io_buffer.h"

namespace Net
{
	IoBuffer::IoBuffer()
		:begin_(0), end_(0)
	{
		memset(data_,0,PacketHeader::PACKET_MAX_SIZE);
	}

	IoBuffer::~IoBuffer()
	{

	}
	
	bool IoBuffer::write_data(char* buffer, ushort size)
	{
		if (get_rest_size() < size)
			return false;

		memcpy(data_ + end_, buffer, size);
		end_ += size;
		return true;
	}

	bool IoBuffer::read_data(char* buffer, ushort size)
	{
		if (get_cur_size() < size)
			return false;

		memcpy(buffer, data_ + begin_, size);
		begin_ += size;
		return true;
	}

	char* IoBuffer::get_data()
	{
		return data_;
	}

	ushort IoBuffer::get_cur_size()
	{
		return end_ - begin_;
	}

	ushort IoBuffer::get_rest_size()
	{
		return PacketHeader::PACKET_MAX_SIZE - end_;
	}
}
